package com.javasm.sale;


import com.javasm.commons.base.BaseController;
import com.javasm.commons.entity.AxiosResult;
import com.javasm.commons.entity.TableDatas;
import com.javasm.sale.service.DeliveryService;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;


@RestController
@RequestMapping("delivery")
public class DeliveryController extends BaseController {
    @Resource
    private DeliveryService deliveryService;

    @GetMapping("/deliveryGoods/{id}")
    public AxiosResult getByid(@PathVariable String id) {
        Delivery obj = this.deliveryService.getDeliveryAndGoodsList(id);
        return suc(obj);
    }

    @GetMapping
    public TableDatas list(Delivery obj) {
        startPage();
        List<Delivery> list = this.deliveryService.listDeliveryAndOrder(obj);
        return toTableDatas(list);
    }

    @PostMapping
    public ResponseEntity add(@RequestBody Order order) throws IOException {
        byte[] bytes =  deliveryService.addAndCreateDeliveryWord(order);
        HttpHeaders headers = new HttpHeaders();
        String fileName = "出库表.docx";
        headers.add("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        return new ResponseEntity(bytes, headers, HttpStatus.OK);
    }

    @PutMapping("/deliveryFinish")
    public AxiosResult updateById(@RequestBody Order order) {
        this.deliveryService.deliveryFinish(order);
        return suc();
    }


    @PutMapping
    public AxiosResult updateById(@RequestBody Delivery delivery) {

        boolean b = this.deliveryService.updateById(delivery);
        return isok(b);
    }

    @DeleteMapping("{ids}")
    public AxiosResult delByIds(@PathVariable String ids) {
        String[] idsArray = ids.split(",");
        boolean b = this.deliveryService.removeByIds(Arrays.asList(idsArray));
        return isok(b);
    }
}

